var utils = require("./utils");

var ad_utils_default_config = {
  ad_min_interval: 40, //最小的看广告时间
  ad_max_go_back_times: 4, //广告结束之后最多的按返回键次数
  if_in_ad: null, //是否在广告页面
  if_in_start: null, //是否在开始广告的页面
  if_in_home: null, //是否在应用首页
  if_has_next_ad: null, //是否有下一个广告
  if_ad_is_over: null, //是否广告已经结束
  exit_ad: null, //退出广告页面
  max_ad_times: 0, //本轮最多的看广告次数
  log: true, //是否打印
};

function create_ad(config) {
  var ad = {
    in_start: false,
    in_home: false,
    watch_ad_times: 0, //已经看的广告的次数
    init() {
      var logger = utils.create_logger("ad init: ");
      if (config.log) {
        console.show();
      }
      logger("");
      this.max_ad_times = random(config.max_ad_times, config.max_ad_times + 1);
      logger("本轮最多看-" + this.max_ad_times + "-个广告");
      this.watch_ad_times = 0;
      var ad_span = config.ad_min_interval + random(0, 5);
      this.run(ad_span);
    },
    run(ad_span) {
      var logger = utils.create_logger("ad run: ");
      var ad_times = this.watch_ad_times + 1;
      logger("看第-" + ad_times + "-个广告，预计耗时-" + ad_span + "-秒");
      var check_span = 2; //每隔2s检查当前广告是否已经结束
      var over_early = false; //广告是否比预计的时间提前结束
      var watch_span = 0; //已经看的时间
      var count = 0; //while循环的执行次数
      while (ad_span > check_span) {
        if (watch_span > 30) {
          if (config.if_in_ad && !config.if_in_ad()) {
            logger("已过-" + watch_span + "-s,但是不在广告页面,将执行back()");
            back();
            sleep(2 * 1000);
          }
        }
        if (config.if_ad_is_over && config.if_ad_is_over()) {
          var wait_over_span = random(2, 5);
          logger("广告已结束,再过" + wait_over_span + "s后停止当前广告");
          sleep(wait_over_span * 1000);
          over_early = true; //广告实际比预计的时间提前结束
          break;
        }
        sleep(check_span * 1000);
        ad_span = ad_span - check_span;
        watch_span = watch_span + check_span;
        count = count + 1;
        if (count % 5 == 0) {
          logger("广告时间预计还剩" + ad_span + "秒");
        }
      }
      if (over_early) {
        this.watch_ad_times = this.watch_ad_times + 1;
        this.go_back();
        return;
      }
      if (ad_span > 0) {
        logger("广告时间还剩最后的-" + ad_span + "-秒");
        sleep(ad_span * 1000);
        this.watch_ad_times = this.watch_ad_times + 1;
        this.go_back();
        return;
      }
    },
    go_back() {
      var logger = utils.create_logger("ad go_back: ");
      logger("");
      var index = 0; //已经按下返回键的次数
      while (index < config.ad_max_go_back_times) {
        back();
        sleep(1 * 1000);
        if (this.watch_ad_times >= this.max_ad_times) {
          logger(
            "本轮看广告次数大于-" + this.max_ad_times + "-" + "将结束本轮看广告"
          );
          config.exit_ad && config.exit_ad();
          sleep(random(1, 2) * 1000);
          this.in_start = true;
          this.after();
          return;
        }
        if (config.if_has_next_ad && config.if_has_next_ad()) {
          var ad_span = config.ad_min_interval + random(0, 5);
          this.run(ad_span);
          return;
        }

        if (config.if_in_start && config.if_in_start()) {
          logger("判断在广告开始页");
          this.in_start = true;
          this.after();
          return;
        }
        if (config.if_in_home && config.if_in_home()) {
          logger("判断在应用首页");
          this.in_home = true;
          this.after();
          return;
        }
        logger("第-" + (index + 1) + "-次执行back");
        index = index + 1;
      }
      this.after();
    },
    after() {
      console.log("ad after");
      this.next();
    },
    next() {
      console.log("ad next");
      if (config.log) {
        console.hide();
      }
    },
  };
  return ad;
}

module.exports = create_ad;
